/**
 *  *2010-6-3 上午12:45:14  add 
 */
package com.base.myproject.client.ui.dialog;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncGetModelDatas;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * 
 * 可以查找的对话框，结果在ok里子类继承进行处理
 *  SelectItemDialog s= new SelectItemDialog();
			 s.setColumndisplayname(new String[]{"编码","名称"});
			 s.setColumnname(new String[]{"bcode","bname"});
			 CommandSyncGetModelDatas csgmd2 = new CommandSyncGetModelDatas();
			 csgmd2.setSqlname("test");
			 s.setCsgmd2(csgmd2);
			 s.show();
 * @author Administrator 2010-6-3 javazgw@gmail.com
 */
public class SelectItemDialog {

	
	String[] columnname;
	String[] columndisplayname;
	Dialog d = new Dialog();
	CommandSyncGetModelDatas csgmd2 = new CommandSyncGetModelDatas();
	/**
	 * 1.单选
	 * 2.多选
	 */
	int selectmode = 1;
	CheckBoxSelectionModel<BusiObject> cbs2 = new CheckBoxSelectionModel<BusiObject>();
	public SelectItemDialog() {
		init();
	}

	private void init() {
		
	}

	public void show()
	{
		createstore();
		createQueryField();
		createGrid();
		d.setButtons(Dialog.OKCANCEL);
		
		
		LayoutContainer lc = new LayoutContainer();
		TableLayout tl = new TableLayout(2);
		tl.setCellPadding(5);
		tl.setCellSpacing(5);
		TableData td1 = new TableData();
		td1.setColspan(1);
		td1.setRowspan(1);
		
		TableData td2 = new TableData();
		td2.setColspan(2);
		td2.setRowspan(1);
		lc.setLayout(tl);
		lc.add(new Label("查找:"),td1);
		lc.add(queryfield,td1);
		lc.add(grid,td2);
		
		d.add(lc);
		
		d.getButtonById(Dialog.OK).setEnabled(false);
		d.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		d.getButtonById(Dialog.CANCEL).addListener(Events.Select, getListener());
		d.setSize(386, 426);
		d.setHeading("请选择");
		d.show();
	}
	protected void close()
	{
		d.hide();
		store = null;
		grid = null;
		d = null;
	}
	/**
	 * 按ok按钮处理，子类继承
	 */
	protected void onOK()
	{
		
	}
	BaseStore<BusiObject> store = new BaseStore<BusiObject>();
	protected BaseGrid<BusiObject> grid;

	void createGrid() {
		
		List<ColumnConfig> columns2 = new ArrayList<ColumnConfig>();
		columns2.add(cbs2.getColumn());

		// ColumnConfig C1 = new ColumnConfig("code", "code", 80);
		// columns.add(C1);
		for(int i=0;i<columnname.length;i++)
		{
			ColumnConfig c1 = new ColumnConfig(columnname[i], columndisplayname[i], 80);
			columns2.add(c1);
		}
		
		ColumnModel cm2 = new ColumnModel(columns2);
		grid = new BaseGrid<BusiObject>(store, cm2, false, false);
		if(getSelectmode()==1)
		cbs2.setSelectionMode(SelectionMode.SINGLE);
		else
		{
			cbs2.setSelectionMode(SelectionMode.MULTI);
		}
		grid.setSelectionModel(cbs2);

		grid.setHeight(300);
		grid.setWidth(353);
		grid.getView().setForceFit(true);
		grid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<BusiObject>(){

			@Override
			public void selectionChanged(SelectionChangedEvent<BusiObject> se) {
				if(se.getSelectedItem()!=null)
				{
				d.getButtonById(Dialog.OK).setEnabled(true);
				}
				else
				{
					d.getButtonById(Dialog.OK).setEnabled(false);
				}
				
			}
			
		});
	}

	void createstore() {
		CommandSyncContainer list2 = new CommandSyncContainer();
		
		
		list2.add(csgmd2);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.SendCommandSync(list2,
				new AsyncCallback<CommandSyncReturnObject>() {

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("错误", "网络连接失败", null);

					}

					@Override
					public void onSuccess(CommandSyncReturnObject result) {
						if (result.isB()) {
						
							if (result.getCommandsync() instanceof CommandSyncGetModelDatas) {

								store.removeAll();
//								grid.getView().setFiresEvents(false);
//								grid.getView().setForceFit(false);
								//TODO 未完成。有bug。时而未能及时刷新
							  	store.setFiresEvents(false);
								store.add(
										((CommandSyncGetModelDatas) (result
												.getCommandsync())).getList(),
										false);
							
								
								store.setFiresEvents(true);
								grid.getView().scrollToTop();
							
								
								grid.el().addStyleName("x-repaint");
								grid.el().removeStyleName("x-repaint");
								
							}
							
						} else {
							MessageBox.alert("错误", "" + result.getMessage(),
									null);
						
						}

					}

				});

	}
	StoreFilterField<BusiObject> queryfield;
	void createQueryField()
	{
		queryfield = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
	
				for (int i = 0; i < columnname.length; i++) {

					Object name = record.get(columnname[i]);
					
					//TODO
					if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
			
				return false;
			}

			@Override
			protected void onFilter() {
				super.onFilter();

			}

		};
		queryfield.bind(store);
		queryfield.setWidth(300);
	}

	
	/**
	 * 1.单选
	 * 2.多选
	 */
	public int getSelectmode() {
		return selectmode;
	}
	/**
	 * 1.单选
	 * 2.多选
	 */
	public void setSelectmode(int selectmode) {
		this.selectmode = selectmode;
	}
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(d.getButtonById(Dialog.OK))) {
								onOK();
							}
							else if (be.getSource().equals(d.getButtonById(Dialog.CANCEL))) {
								close();
							}
						
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	public String[] getColumnname() {
		return columnname;
	}

	public void setColumnname(String[] columnname) {
		this.columnname = columnname;
	}

	public String[] getColumndisplayname() {
		return columndisplayname;
	}

	public void setColumndisplayname(String[] columndisplayname) {
		this.columndisplayname = columndisplayname;
	}

	public CommandSyncGetModelDatas getCsgmd2() {
		return csgmd2;
	}

	public void setCsgmd2(CommandSyncGetModelDatas csgmd2) {
		this.csgmd2 = csgmd2;
	}

	public BaseStore<BusiObject> getStore() {
		return store;
	}

	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}

	public BaseGrid<BusiObject> getGrid() {
		return grid;
	}

	public void setGrid(BaseGrid<BusiObject> grid) {
		this.grid = grid;
	}

	public StoreFilterField<BusiObject> getQueryfield() {
		return queryfield;
	}

	public void setQueryfield(StoreFilterField<BusiObject> queryfield) {
		this.queryfield = queryfield;
	}

}
